package com.ymatou.iapi.godmch.testcase;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.godmch.parameter.CreateCodeBean;
import com.ymatou.iapi.godmch.parameter.applyAgentBean;
import com.ymatou.iapi.godmch.parameter.approveAgentBean;
import com.ymatou.iapi.godmch.parameter.createIndepMchBean;
import com.ymatou.iapi.godmch.parameter.getAgentStatusBean;
import com.ymatou.iapi.godmch.parameter.reApplyAgentBean;
import com.ymatou.iapi.godmch.parameter.rejectAgentBean;
import com.ymatou.iapi.godmch.service.CreateCodeCall;
import com.ymatou.iapi.godmch.service.applyAgentCall;
import com.ymatou.iapi.godmch.service.createIndepMchCall;
import com.ymatou.iapi.godmch.service.getAgentStatusCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.shenqi.GodmchManageCallService;
import com.ymttest.database.model.agent_type;
import com.ymttest.database.model.mch_relation;
import com.ymttest.database.sqlwapper.godmch.agent_typeWapper;
import com.ymttest.database.sqlwapper.godmch.country_infoWapper;
import com.ymttest.database.sqlwapper.godmch.mch_infoWapper;
import com.ymttest.database.sqlwapper.godmch.mch_relationWapper;
/**
*代理商状态查询
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_getAgentStatus{
	private static getAgentStatusBean bean;
	private static getAgentStatusCall call;
	
	agent_typeWapper agentTypeWapper=new agent_typeWapper();
	mch_infoWapper infoWapper=new mch_infoWapper();
	country_infoWapper country_infoWapper=new country_infoWapper();
	mch_relationWapper relationWapper=new mch_relationWapper();
	
	
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("代理商状态查询");
	}

	@Before
	public void caseUp() {
		bean=new getAgentStatusBean();
		call=new getAgentStatusCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(call.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_getAgentStatus_001() {
		Logger.start(true,"正例-代理商状态查询-状态正常");
		try {
			
			//代理申请
			applyAgentBean applyBean=GodmchManageCallService.InitApplyAgentBean();
			applyAgentCall applyCall=GodmchManageCallService.ApplyAgentCall(applyBean);
			//查询代理类型
			Map<String,Object> map=new HashMap();
			map.put("mchId", applyBean.getMchId());
			List<agent_type> list=agentTypeWapper.selectby(map);
			//审核通过
			approveAgentBean approveBean=GodmchManageCallService.InitApproveAgentBean();
			approveBean.setMchId(applyBean.getMchId());
			approveBean.setAgentId(applyCall.getAgentId());
			approveBean.setAgentType(list.get(0).getAgentType());
			GodmchManageCallService.ApproveAgentCall(approveBean);
			
			bean.setAgentId(applyCall.getAgentId());
			bean.setMchId(applyBean.getMchId());
			call.setData(bean);
			call.callService();			
			
			Logger.verifyEquals("200", call.getStatusCode1(), "验证StatusCode");
			Logger.verifyEquals(1, call.getStatus(), "验证status");	
			
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_getAgentStatus_002() {
		Logger.start(true,"正例-代理商状态查询-状态待审核");
		try {
			//代理申请
			applyAgentBean applyBean=GodmchManageCallService.InitApplyAgentBean();
			applyAgentCall applyCall=GodmchManageCallService.ApplyAgentCall(applyBean);
			
			bean.setAgentId(applyCall.getAgentId());
			bean.setMchId(applyBean.getMchId());
			call.setData(bean);
			call.callService();			
			
			Logger.verifyEquals("200", call.getStatusCode1(), "验证StatusCode");
			Logger.verifyEquals(2, call.getStatus(), "验证status");			
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_getAgentStatus_003() {
		Logger.start(true,"正例-代理商状态查询-状态已删除");
		try {
			//代理申请
			applyAgentBean applyBean=GodmchManageCallService.InitApplyAgentBean();
			applyAgentCall applyCall=GodmchManageCallService.ApplyAgentCall(applyBean);
			//查询代理类型
			Map<String,Object> map=new HashMap();
			map.put("mchId", applyBean.getMchId());
			List<agent_type> list=agentTypeWapper.selectby(map);
			//审核通过
			approveAgentBean approveBean=GodmchManageCallService.InitApproveAgentBean();
			approveBean.setMchId(applyBean.getMchId());
			approveBean.setAgentId(applyCall.getAgentId());
			approveBean.setAgentType(list.get(0).getAgentType());
			GodmchManageCallService.ApproveAgentCall(approveBean);
			//移除关系
			GodmchManageCallService.InitremovemchForAgent(applyBean.getMchId(),applyCall.getAgentId());
			
			bean.setMchId(applyBean.getMchId());
			bean.setAgentId(applyCall.getAgentId());
			call.setData(bean);
			call.callService();				
			
			Logger.verifyEquals("200", call.getStatusCode1(), "验证StatusCode");
			Logger.verifyEquals(3, call.getStatus(), "验证status");	
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_getAgentStatus_004() {
		Logger.start(true,"正例-代理商状态查询-状态拒绝");
		try {
			
			//代理申请
			applyAgentBean applyBean=GodmchManageCallService.InitApplyAgentBean();
			applyAgentCall applyCall=GodmchManageCallService.ApplyAgentCall(applyBean);
			//拒绝
			rejectAgentBean rejectBean=GodmchManageCallService.initRejectAgentBean();
			rejectBean.setMchId(applyBean.getMchId());
			rejectBean.setAgentId(applyCall.getAgentId());
			GodmchManageCallService.rejectAgentCall(rejectBean);
			
			bean.setMchId(applyBean.getMchId());
			bean.setAgentId(applyCall.getAgentId());
			call.setData(bean);
			call.callService();				
			
			Logger.verifyEquals("200", call.getStatusCode1(), "验证StatusCode");
			Logger.verifyEquals(4, call.getStatus(), "验证status");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_getAgentStatus_005() {
		Logger.start(true,"正例-代理商状态查询-状态重新申请被拒绝");
		try {
			//代理申请
			applyAgentBean applyBean=GodmchManageCallService.InitApplyAgentBean();
			applyAgentCall applyCall=GodmchManageCallService.ApplyAgentCall(applyBean);
			//查询代理类型
			Map<String,Object> map=new HashMap();
			map.put("mchId", applyBean.getMchId());
			List<agent_type> list=agentTypeWapper.selectby(map);
			//审核通过
			approveAgentBean approveBean=GodmchManageCallService.InitApproveAgentBean();
			approveBean.setMchId(applyBean.getMchId());
			approveBean.setAgentId(applyCall.getAgentId());
			approveBean.setAgentType(list.get(0).getAgentType());
			GodmchManageCallService.ApproveAgentCall(approveBean);
			//移除关系
			GodmchManageCallService.InitremovemchForAgent(applyBean.getMchId(),applyCall.getAgentId());
			//重新申请
			reApplyAgentBean reApplyAgentBean=GodmchManageCallService.initReApplyAgentBean(applyBean.getMchId(),applyCall.getAgentId());
			GodmchManageCallService.reApplyAgentCall(reApplyAgentBean);
			
			//拒绝
			rejectAgentBean rejectBean=GodmchManageCallService.initRejectAgentBean();
			rejectBean.setMchId(applyBean.getMchId());
			rejectBean.setAgentId(applyCall.getAgentId());
			GodmchManageCallService.rejectAgentCall(rejectBean);
			
			bean.setMchId(applyBean.getMchId());
			bean.setAgentId(applyCall.getAgentId());
			call.setData(bean);
			call.callService();	
			
			Logger.verifyEquals("200", call.getStatusCode1(), "验证StatusCode");
			Logger.verifyEquals(5, call.getStatus(), "验证status");
			Logger.verifyEquals(rejectBean.getReason(), call.getRemark(), "验证Remark");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_getAgentStatus_006() {
		Logger.start(true,"正例-代理商状态查询-agentId不存在");
		try {
			//代理申请
			applyAgentBean applyBean=GodmchManageCallService.InitApplyAgentBean();
			applyAgentCall applyCall=GodmchManageCallService.ApplyAgentCall(applyBean);
			//查询代理类型
			Map<String,Object> map=new HashMap();
			map.put("mchId", applyBean.getMchId());
			List<agent_type> list=agentTypeWapper.selectby(map);
			//审核通过
			approveAgentBean approveBean=GodmchManageCallService.InitApproveAgentBean();
			approveBean.setMchId(applyBean.getMchId());
			approveBean.setAgentId(applyCall.getAgentId());
			approveBean.setAgentType(list.get(0).getAgentType());
			GodmchManageCallService.ApproveAgentCall(approveBean);
			//移除关系
			GodmchManageCallService.InitremovemchForAgent(applyBean.getMchId(),applyCall.getAgentId());
			//重新申请
			reApplyAgentBean reApplyAgentBean=GodmchManageCallService.initReApplyAgentBean(applyBean.getMchId(),applyCall.getAgentId());
			GodmchManageCallService.reApplyAgentCall(reApplyAgentBean);
			
			bean.setMchId(applyBean.getMchId());
			bean.setAgentId(applyCall.getAgentId());
			call.setData(bean);
			call.callService();	
			
			Logger.verifyEquals("200", call.getStatusCode1(), "验证StatusCode");
			Logger.verifyEquals(6, call.getStatus(), "验证status");
			//Logger.verifyEquals(rejectBean.getReason(), call.getRemark(), "验证Remark");		
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_getAgentStatus_007() {
		Logger.start(true,"正例-代理商状态查询-agentId为空");
		try {
			//代理申请
			applyAgentBean applyBean=GodmchManageCallService.InitApplyAgentBean();
			applyAgentCall applyCall=GodmchManageCallService.ApplyAgentCall(applyBean);
			
			bean.setMchId(applyBean.getMchId());
			bean.setAgentId(null);
			call.setData(bean);
			call.callService();			
			
			Logger.verifyEquals("4001", call.getStatusCode1(), "验证StatusCode");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_getAgentStatus_008() {
		Logger.start(true,"正例-代理商状态查询-独立商户申请代理-代理申请代理-状态正常");
		try {
			
			 ///////////////////// 主商户招代理////////////////////////
			createIndepMchBean createBean=GodmchManageCallService.InitCreateIndepMchBean();
			createIndepMchCall createCall=GodmchManageCallService.CreateIndepMchCall(createBean);
			//独立商户邀请码
			CreateCodeBean createCodeBean=GodmchManageCallService.InitcreateCodeBeanForAgent(createCall.getmchId(), true);
			CreateCodeCall createCodeCall=GodmchManageCallService.createCodeCall(createCodeBean);
			Thread.sleep(5000);
			applyAgentBean applyAgentBean=GodmchManageCallService.InitApplyAgentBean();
			applyAgentBean.setMchId(createCall.getmchId());
			applyAgentBean.setAppKey("appsqSeller_"+createCall.getmchId());
			applyAgentBean.setInviteCode(createCodeCall.getCode());
			applyAgentCall applyAgentCall=GodmchManageCallService.ApplyAgentCall(applyAgentBean);
			
			Logger.comment("MchId1:"+createCodeBean.getMchId());
			Logger.comment("Agent1:"+applyAgentCall.getAgentId());
			
			//查询代理类型
			Map<String,Object> map=new HashMap();
			map.put("mchId", createCodeBean.getMchId());
			List<agent_type> list=agentTypeWapper.selectby(map);
			//mch_relation
			mch_relation relation=relationWapper.selectByPrimaryKey(applyAgentCall.getAgentId(), applyAgentBean.getMchId()).get(0);
			Logger.verifyEquals(list.get(0).getAgentType(), relation.getAgentType(), "验证AgentType");
			Logger.verifyEquals(1, relation.getAgentModel(), "验证AgentModel");
			Logger.verifyEquals(0, relation.getState(), "验证state");
			 /////////////////////// 代理招代理////////////////////////
			
			//代理申请代理
		    createCodeBean=GodmchManageCallService.InitcreateCodeBeanForAgent(applyAgentCall.getAgentId(),true);
			createCodeCall=GodmchManageCallService.createCodeCall(createCodeBean);
			Thread.sleep(5000);
			applyAgentBean=GodmchManageCallService.InitApplyAgentBean();
			//bean.setMchId(call.getAgentId());
			applyAgentBean.setMchId(createCall.getmchId());
			applyAgentBean.setAppKey("appsqSeller_"+createCall.getmchId());
			applyAgentBean.setInviteCode(createCodeCall.getCode());
			applyAgentCall=GodmchManageCallService.ApplyAgentCall(applyAgentBean);
			
			Logger.comment("MchId2:"+createCodeBean.getMchId());
			Logger.comment("Agent2:"+applyAgentCall.getAgentId());
			
			bean.setAgentId(applyAgentCall.getAgentId());
			bean.setMchId(createCall.getmchId());
			call.setData(bean);
			call.callService();			
			
			Logger.verifyEquals("200", call.getStatusCode1(), "验证StatusCode");
			Logger.verifyEquals(1, call.getStatus(), "验证status");	
			
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}